<template>
  <div>
    <el-form
      slot:default
      :model="form"
      :rules="rules"
      label-width="100px"
      ref="form"
      class="kylin-form"
    >
      <el-form-item :label="$t('user.role_name')" prop="name">
        <el-input
          v-model="form.name"
          autocomplete="off"
          :disabled="type === 'update'"
        ></el-input>
      </el-form-item>
      <el-form-item :label="$t('user.role_description')" prop="description">
        <el-input
          maxlength="50"
          v-model="form.description"
          autocomplete="off"
        ></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="handleCancel">{{ $t("cancel") }}</el-button>
      <el-button class="kylin-button" @click="handleOk">{{
        $t("confirm")
      }}</el-button>
    </div>
  </div>
</template>

<script>
import { createRoles, updateRole, getRole, copyRole } from "@/api/role";
export default {
  props: {
    rowId: {
      type: Number | String,
      default: "",
    },
    type: {
      type: String,
      default: "",
    },
  },
  data() {
    let checkName = (rule, value, callback) => {
      let reg = /[\\|\（|\\|\/|:|\*|?|\"|\<|\>|\||\）]/;
      if (!value) {
        return callback(new Error(this.$t("user.role_name_require")));
      } else if (reg.test(value)) {
        return callback(
          new Error(this.$t("user.role_name_content_error") + '（/:*?"<>|）')
        );
      } else {
        return callback();
      }
    };
    return {
      roles: [],
      form: {
        name: "",
        description: "",
      },
      rules: {
        name: [
          {
            required: true,
            validator: checkName,
            trigger: "change",
          },
        ],
      },
    };
  },
  mounted() {
    if (this.rowId) {
      getRole({ id: this.rowId }).then((res) => {
        if (res.data.code == "0") {
          this.form.name = res.data.data.name;
          this.form.description = res.data.data.description;
        } else {
          this.$message({
            type: "error",
            message: res.data.message,
          });
        }
      });
    }
  },
  methods: {
    handleCancel() {
      this.$refs.form.resetFields();
      this.$emit("click");
    },
    handleOk() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          if (this.type === "update") {
            let data = { ...this.form, id: this.rowId };
            updateRole(data).then((res) => {
              if (res.data.code == "0") {
                this.$message({
                  type: "success",
                  message: res.data.message,
                });
                this.$emit("click", "success");
              } else {
                this.$message({
                  type: "error",
                  message: res.data.message,
                });
              }
            });
          } else if (this.type === "copy") {
            let data = { ...this.form, id: this.rowId };
            copyRole(data).then((res) => {
              if (res.data.code == "0") {
                this.$message({
                  type: "success",
                  message: res.data.message,
                });
                this.$emit("click", "success");
              } else {
                this.$message({
                  type: "error",
                  message: res.data.message,
                });
              }
            });
          } else if (this.type === "create") {
            createRoles({ ...this.form }).then((res) => {
              if (res.data.code == "0") {
                this.$message({
                  type: "success",
                  message: res.data.message,
                });
                this.$emit("click", "success");
              } else {
                this.$message({
                  type: "error",
                  message: res.data.message,
                });
              }
            });
          }
        }
      });
    },
  },
};
</script>